/*
 * init_lhdfs - Initial the lhdfs
 * @param uri      the palce where is the filesystem placed
 * @param fs_name  the name of the filesystem
 * @return Return a handle to the lhdfs
 */
#include <glib.h>
#include "hlfs.h"
#include "comm_define.h"


#if 1
gboolean is_fs_accessable(const char* uri,const char *fs_name){

     gchar **v  = g_strsplit(uri,"://",2);
     if (0==g_strcmp(v[0],"local")){
         gchar * fs_path = g_build_filename(v[1],fs_name,NULL);
         g_message(G_LOG_HLFS_DOMAIN,"local [fs:%s] [path%s]\n",fs_name,fs_path);
         if (!g_file_test (fs_path, G_FILE_TEST_IS_DIR)){
             g_free(fs_path);
             g_strfreev(v);
             return FALSE;
         }
         g_free(fs_path);
     }else if(0 == g_strcmp(v[0],"hdfs")){
         g_message(G_LOG_HLFS_DOMAIN,"hdfs storage not implement now \n");
     }else{
         g_message(G_LOG_HLFS_DOMAIN,"Error URI \n");
         g_strfreev(v);
         return FALSE;
     }
     g_strfreev(v);
     return TRUE;
}
#endif 

int read_fs_superblock(const char *fs_path,struct super_block *sb){
    int ret;
    gchar *sb_file_path = g_build_filename(fs_path,"superblock",NULL);
    if (!g_file_test(sb_file_path, G_FILE_TEST_EXISTS) || !g_file_test(sb_file_path,G_FILE_TEST_IS_REGULAR)){
        g_message(G_LOG_HLFS_DOMAIN,"superblock file %s can not be accessable \n",sb_file_path);
        g_free(sb_file_path);
        return -1;
    }  
    GKeyFile * sb_keyfile = g_key_file_new();
    if(FALSE == g_key_file_load_from_file(sb_keyfile,sb_file_path,G_KEY_FILE_NONE,NULL)){
       g_message(G_LOG_HLFS_DOMAIN,"superblock file %s format is not key value pairs \n",sb_file_path);
       g_free(sb_file_path);
       g_key_file_free(sb_keyfile);
       return  -2;
    }
    gchar * fs_name =  g_key_file_get_string(sb_keyfile,"METADATA","fs_name",NULL);
    guint32 seg_size = g_key_file_get_integer(sb_keyfile,"METADATA","segment_size",NULL);
    guint32 block_size = g_key_file_get_integer(sb_keyfile,"METADATA","block_size",NULL);
   
    if(fs_name==NULL || seg_size == 0 || block_size ==0 ){
       g_message(G_LOG_HLFS_DOMAIN,"superblock parse error \n");
       g_free(sb_file_path);
       g_key_file_free(sb_keyfile);
       return -3;
    }
    sb->seg_size = seg_size;
    sb->block_size = block_size;
    gi_strlcpy(sb->fsname,fs_name,MAX_FILE_NAME_LEN);
    g_free(sb_file_path);
    g_key_file_free(sb_keyfile);
    return 0;
}


struct hlfs_ctrl *
init_lhdfs(const char *uri, const char *fs_name)
{
	int ret;
    if(uri == NULL && fs_name == NULL){
       g_message(G_LOG_HLFS_DOMAIN,"input error \n");
       return NULL;
    }
    if(FALSE == is_fs_accessable(uri,fs_name)){
       g_message(G_LOG_HLFS_DOMAIN,"[uri:%s] [fs:%s] can not accessable\n",uri,fs_name);
       return NULL;
    }

    gchar **v  = g_strsplit(uri,"://",2);
    if (0==g_strcmp(v[0],"local")){
         gchar * fs_path = g_build_filename(v[1],fs_name,NULL);
         struct super_block sb;
         ret = read_fs_superblock(fs_name,&sb);
    }
#if 0
    if( is_uri_accessable(uri) && is_fs_accessable(fs_name) )
	{        
		 ret = read_fs_superblock(uri, fs_name, &sb);
         if(ret != 0)
         	return NULL;
					                
         lhdfs_ctrl *ctrl = build_fs_control_block(sb, imp);
		 ret = init_lhdfs_file(ctrl, uri fs_name);
	 	 if(ret != 0)
	        return NULL;
	}				              
	       
	return ctrl;
#endif
    return NULL;
}   

